package com.mayisoftware.mayioa.business.plan.impl.serviceImpl;

import com.mayisoftware.mayioa.business.plan.api.domain.PlanClass;
import com.mayisoftware.mayioa.business.plan.api.domain.PlanDetailConfig;
import com.mayisoftware.mayioa.business.plan.api.service.IPlanClassService;
import com.mayisoftware.mayioa.business.plan.api.service.IPlanConfigDetailService;
import com.mayisoftware.mayioa.business.plan.impl.dao.PlanClassMapper;
import com.mayisoftware.mayioa.common.impl.support.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 计划课程 业务处理层
 */
@Service
public class PlanClassServiceImpl implements IPlanClassService {
    @Autowired
    private PlanClassMapper planClassMapper;

    @Autowired
    private IPlanConfigDetailService planDetailService;

    /**
     * 根据条件分页查询计划课程
     * @param planClass 计划课程信息
     * @return 计划课程列表
     */
    @Override
    public List<PlanClass> selectPlanClassList(PlanClass planClass) {
        return planClassMapper.selectPlanClassList(planClass);
    }

    /**
     * 查询不属于当前计划的课程
     * @param planCfgId 计划课程信息
     * @return 计划课程列表
     */
    @Override
    public List<PlanClass> selectPlanClassList(Long planCfgId) {
        return planClassMapper.selectPlanClassListByPlanId(planCfgId);
    }

    /**
     * 通过计划课程ID查询计划课程信息
     * @param planClassId 计划课程ID
     * @return 计划课程信息
     */
    @Override
    public PlanClass selectPlanClassById(Long planClassId) {
        return planClassMapper.selectPlanClassById(planClassId);
    }
    /**
     * 插入计划课程
     * @param planClass 计划课程信息
     * @return
     */
    @Override
    public int insertPlanClass(PlanClass planClass) {
        return planClassMapper.insertPlanClass(planClass);
    }
    /**
     * 修改计划课程信息
     * @param planClass 计划课程信息
     * @return 结果
     */
    @Override
    public int updatePlanClass(PlanClass planClass) {
        planDetailService.updatePlanItemName(planClass.getPlanClassId(), planClass.getPlanClassName());
        return planClassMapper.updatePlanClass(planClass);
    }

    /**
     * 批量删除计划课程信息
     * @param ids 计划课程id字符串
     * @return 结果
     * @throws Exception
     */
    @Override
    public int deletePlanClassByIds(String ids) throws Exception {
        Long[] idArray = Convert.toLongArray(ids);
        List<Long> removeIds = new ArrayList<Long>();
        for(Long id : idArray){
            List<PlanDetailConfig> inPlan = planDetailService.selectPlanItemByCfgItemIdAndCfgItemType(1,id.intValue());
            //查询课程id不在计划中
            if (inPlan.isEmpty()){
                removeIds.add(id);
            }
        }
        return planClassMapper.deletePlanClassByIds((Long[]) removeIds.toArray());
    }


}
